<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="common/include :: header('系统访问记录列表')"/>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" lay-filter="login-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">登录账号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="loginName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">登录时间</label>
                            <div class="layui-inline">
                                <div class="layui-input-inline">
                                    <input type="text" autocomplete="off" id="loginTime" name="loginTime"
                                           class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
                <button class="layui-btn layui-btn-sm layui-btn-normal data-add-btn" lay-event="clear"> 清空</button>
                <button class="layui-btn layui-btn-sm layui-btn-green data-add-btn" lay-event="unlock"> 解锁</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

    </div>
</div>
<th:block th:include="common/include :: footer"/>

<script th:inline="javascript">
    layui.use(['form', 'table', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate;
        ;

        //日期范围
        laydate.render({
            elem: '#loginTime'
            //设置开始日期、日期日期的 input 选择器
            , range: '~'
        });

        table.render({
            elem: '#currentTableId',
            url: ctx + 'sysLoginLog/pageList',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            parseData: function (res) {
                return res.data;
            },
            cols: [
                [
                    {type: "checkbox"},
                    {field: 'loginName', title: '登录账号', sort: true},
                    {field: 'ipAddress', title: '登录IP地址', sort: true},
                    {field: 'loginLocation', title: '登录地点', sort: true},
                    {field: 'browser', title: '浏览器类型', sort: true},
                    {field: 'os', title: '操作系统', sort: true},
                    {
                        field: 'status', title: '状态', templet: function (d) {
                            if (d.status == '1') {
                                return '<span class="layui-badge layui-bg-red">失败</span>';
                            } else {
                                return '<span class="layui-badge layui-bg-green">成功</span>';
                            }
                        }
                    },
                    {field: 'msg', title: '提示消息', sort: true},
                    {field: 'loginTime', title: '访问时间', sort: true}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 搜索
        function search() {
            var result = JSON.stringify(form.val('login-form'));
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');
        }

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            search();
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'clear') {  // 监听添加操作
                layer.confirm('确定清空访问日志吗?', function (index) {
                    layer.close(index);
                    $.ajax({
                        url: ctx + 'sysLoginLog/clear',
                        type: 'GET',
                        async: false,
                        dataType: "json",
                        success: function (res) {
                            layer.msg(res.msg);
                            search();
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            if (XMLHttpRequest.status == 404) {
                                window.location.href = ctx + "/404";
                            } else {
                                layer.msg("服务器好像出了点问题！请稍后试试");
                            }
                        }
                    });
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data != null && data.length > 0) {
                    layer.confirm('真的删除行么', function (index) {
                        layer.close(index);

                        var ids = [];
                        for (var i = 0; i < data.length; i++) {
                            ids[i] = data[i].id;
                        }

                        $.ajax({
                            url: ctx + 'sysLoginLog/batchDelete',
                            type: 'POST',
                            async: false,
                            data: JSON.stringify(ids),
                            contentType: 'application/json; charset=UTF-8',
                            dataType: "json",
                            success: function (res) {
                                layer.msg(res.msg);
                                search();
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                layer.close(index);
                                if (XMLHttpRequest.status == 404) {
                                    window.location.href = ctx + "/404";
                                } else {
                                    layer.msg("服务器好像出了点问题！请稍后试试");
                                }
                            }
                        });
                    });
                } else {
                    layer.alert("请先选择行");
                }
            } else if (obj.event === 'unlock') { // 解锁用户
                layer.confirm('确定解锁该用户吗?', function (index) {
                    layer.close(index);
                    var checkStatus = table.checkStatus('currentTableId')
                        , data = checkStatus.data;

                    var usernames = [];
                    for (var i = 0; i < data.length; i++) {
                        usernames[i] = data[i].loginName;
                    }
                    $.ajax({
                        url: ctx + 'sysLoginLog/unlock',
                        type: 'POST',
                        async: false,
                        dataType: "json",
                        data: JSON.stringify(usernames),
                        contentType: 'application/json; charset=UTF-8',
                        success: function (res) {
                            layer.msg(res.msg);
                            search();
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            if (XMLHttpRequest.status == 404) {
                                window.location.href = ctx + "/404";
                            } else {
                                layer.msg("服务器好像出了点问题！请稍后试试");
                            }
                        }
                    });
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            // console.log(obj)
        });
    });
</script>